<?php
// 引入配置文件 
include_once('config/config.php');

$LoginAdmin = isset($_COOKIE['LoginAdmin']) ? json_decode($_COOKIE['LoginAdmin'],true) : [];

if($LoginAdmin)
{
    $id = $LoginAdmin['id'] ?? 0;
    $username = $LoginAdmin['username'] ?? '';

    $admin = find("SELECT * FROM `pre_admin` WHERE `id` = '$id' AND `username` = '$username'");

    if(!$admin)
    {
        // 清除cookie
        setcookie('LoginAdmin','',time() - 1);
        showMsg('非法登录','login.php');
    }

    showMsg('无须重复登录','index.php');
}

// 判断是否有POST提交
if($_POST)
{
    // 获取参数
    $username = $_POST['username'] ?? '';
    $password = $_POST['password'] ?? '';
    $code = $_POST['code'] ?? '';

    if($code != $_SESSION['code'])
    {
        showMsg('验证码输入错误');
    }

    $admin = find("SELECT * FROM `pre_admin` WHERE `username` = '$username'");

    if(!$admin)
    {
        showMsg('用户名不存在');
    }

    $password = md5($password.$admin['salt']);

    if($password != $admin['password'])
    {
        showMsg('密码错误');
    }

    // 存储登录信息 组装数据
    $data = [
        'id' => $admin['id'],
        'username' => $admin['username'],
        'avatar' => $admin['avatar'],
    ];

    // 设置cookie
    setcookie('LoginAdmin',json_encode($data));

    showMsg('登录成功','index.php');
}
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <?php include_once('common/meta.php'); ?>
  <body>
    
    <div class="navbar">
        <div class="navbar-inner">
            <a class="brand" href="index.php"><span class="second">Admin</span></a>
        </div>
    </div>

    <div class="row-fluid">
        <div class="dialog">
            <div class="block">
                <p class="block-heading">登录</p>
                <div class="block-body">
                    <form method="post">
                        <label>用户名</label>
                        <input type="text" name="username" placeholder="请输入用户名" class="span12">
                        
                        <label>密码</label>
                        <input type="password" name="password" placeholder="请输入密码" class="span12">
                        
                        <!-- 验证码 -->
                        <label>密码</label>
                        <input type="text" name="code" placeholder="请输入验证码" class="span6">
                        <img src="/config/imgcode.php" onclick="this.src = '/config/imgcode.php?id='+ Math.random()" alt="">

                        <!-- 登录按钮 -->
                        <button type="submit" class="btn btn-primary pull-right">登录</a>
                        <div class="clearfix"></div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    
  </body>
</html>
<?php include_once('common/script.php') ?>

<script>
    // 提交做了一个点击事件
    $('button[type="submit"]').click(function(){
        // 监听用户名，密码以及验证码是否为空
        if(!$.trim($('input[name="username"]').val()))
        {
            alert('请输入用户名');
            return false;
        }

        if(!$.trim($('input[name="password"]').val()))
        {
            alert('请输入密码');
            return false;
        }

        if(!$.trim($('input[name="code"]').val()))
        {
            alert('请输入验证码');
            return false;
        }
    });
</script>